package cn.myfirefly.moming.common.dao;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import cn.myfirefly.moming.common.dao.mapper.StaffMapper;
import cn.myfirefly.moming.common.entity.MMStaff;
import cn.myfirefly.moming.common.model.StaffDto;
import cn.myfirefly.moming.common.model.StaffFilterDto;
import cn.myfirefly.core.BaseDao;
import cn.myfirefly.core.model.PageResultDto;
import cn.myfirefly.core.mybatis.BjuiDataGridFilter;
import cn.myfirefly.core.mybatis.BjuiDataGridSqlHelper;
import cn.myfirefly.utils.StrUtil;

@Repository
public class StaffDao extends BaseDao<MMStaff> {
	
	@Autowired
	private StaffMapper staffMapper;
	
	public PageResultDto<StaffDto> getList(StaffFilterDto dto) {
		
		PageResultDto<StaffDto> resultDto = new PageResultDto<>();
		//参数处理
		Map<String, Object> paraMap = new HashMap<>();
		BjuiDataGridFilter bjuiDataGridFilter = BjuiDataGridSqlHelper.addFilter(dto);
		paraMap.putAll(bjuiDataGridFilter.getParam());
		//分页处理
		Page<StaffDto> page = PageHelper.startPage(dto.getPageCurrent(), dto.getPageSize(), true);
		List<StaffDto> list = staffMapper.getList(paraMap);
		resultDto.setPageCurrent(dto.getPageCurrent());
		resultDto.setTotal((int)page.getTotal());
		resultDto.setList(list);
		return resultDto;
	}
	
	public PageResultDto<StaffDto> getList(StaffDto dto) {
		
		PageResultDto<StaffDto> resultDto = new PageResultDto<>();
		//参数处理
		String name = null;
		if (StrUtil.notBlank(dto.getName())) {
			name = "%" + dto.getName() + "%";
		}
		//分页处理
		Page<StaffDto> page = PageHelper.startPage(dto.getPageCurrent(), dto.getPageSize(), true);
		List<StaffDto> list = staffMapper.getListByName(name);
		resultDto.setPageCurrent(dto.getPageCurrent());
		resultDto.setTotal((int)page.getTotal());
		resultDto.setList(list);
		
		return resultDto;
	}
	
	@SuppressWarnings("unchecked")
	public MMStaff getStaffByNo(String staffNo) {
		Session session = sessionFactory.getCurrentSession();
		MMStaff staff = null;
		Query query = session.createQuery("from MMStaff u where u.staffNo=? ");
		query.setString(0, staffNo);
		List<MMStaff> list = query.list();
		if (list != null && list.size() > 0) {
			staff = list.get(0);
		}
		return staff;
	}
	
	@SuppressWarnings("unchecked")
	public MMStaff getStaffByUserSn(Integer userSn) {
		Session session = sessionFactory.getCurrentSession();
		MMStaff staff = null;
		Query query = session.createQuery("from MMStaff u where u.userSn=? ");
		query.setInteger(0, userSn);
		List<MMStaff> list = query.list();
		if (list != null && list.size() > 0) {
			staff = list.get(0);
		}
		return staff;
	}
	
	public StaffDto getStaffByLoginName(String loginName) {
		return staffMapper.getStaffByLoginName(loginName);
	}
	
	@SuppressWarnings("unchecked")
	public List<MMStaff> getStaff() {
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createQuery(" from MMStaff where userStatus!='2' ");
		List<MMStaff> list = query.list();
		return list;
	}
	
	@SuppressWarnings("unchecked")
	public List<MMStaff> getAllStaff() {
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createQuery(" from MMStaff ");
		List<MMStaff> list = query.list();
		return list;
	}
}
